import 'package:flutter/material.dart';

import '../res.dart';

class MyHomePage extends StatefulWidget {
    MyHomePage({Key key, this.title}) : super(key: key);

    // This widget is the home page of your application. It is stateful, meaning
    // that it has a State object (defined below) that contains fields that affect
    // how it looks.

    // This class is the configuration for the state. It holds the values (in this
    // case the title) provided by the parent (in this case the App widget) and
    // used by the build method of the State. Fields in a Widget subclass are
    // always marked "final".

    final String title;

    @override
    _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
    @override
    Widget build(BuildContext context) {
        // This method is rerun every time setState is called, for instance as done
        // by the _incrementCounter method above.
        //
        // The Flutter framework has been optimized to make rerunning build methods
        // fast, so that you can just rebuild anything that needs updating rather
        // than having to individually change instances of widgets.
        return Scaffold(
                body: Container(
                    child: Stack(
                        children: <Widget>[
                            Image.asset(Res.bg_1),
                            SafeArea(
                                child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: <Widget>[
                                        buildTitleBar(),
                                        buildHeaderInfo(),
                                        SingleChildScrollView(
                                            child: Column(
                                                crossAxisAlignment: CrossAxisAlignment.start,
                                                children: <Widget>[
                                                    //                创建label
                                                    buildLabel('常用功能'),
                                                    //                创建单行常用功能菜单
                                                    buildCard([
                                                        buildCardItem(
                                                                name: '积分充值', iconAssets: Res.icon_integral),
                                                        buildCardItem(
                                                                name: '储值充值', iconAssets: Res.icon_store_value),
                                                        buildCardItem(name: '订单验证', iconAssets: Res.icon_order)
                                                    ]),
                                                    SizedBox(
                                                        height: 20,
                                                    ),
                                                    buildLabel('营销工具核销'),
                                                    buildYxgjHx([
                                                        GridItem('砍价', Res.icon_bargain, (name) {
                                                            print('我是回调$name');
                                                        }),
                                                        GridItem('添柴', Res.icon_fire, (name) {
                                                            print('我是回调$name');
                                                        }),
                                                        GridItem('福多多', Res.icon_blessing, (name) {
                                                            print('我是回调$name');
                                                        }),
                                                        GridItem('引流飞轮', Res.control_turntable, (name) {
                                                            print('我是回调$name');
                                                        }),
                                                        GridItem('拼团', Res.icon_team, (name) {
                                                            print('我是回调$name');
                                                        }),
                                                    ]),
                                                ],
                                            ),
                                        )
                                    ],
                                ),
                            )
                        ],
                    ),
                ));
    }

    Container buildYxgjHx(List<GridItem> item,[int itemCount=5,double viewHeight=190.0]) {
        return Container(
            height: viewHeight,
            padding: EdgeInsets.only(left: 12, right: 12),
            child: Card(
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
                child: Padding(
                    padding: EdgeInsets.all(10),
                    child: GridView.builder(
                            gridDelegate:
                            SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4),
                            primary: false,
                            itemCount: itemCount,
                            itemBuilder: (context, index) {
                                var itemBean = item[index];
                                return GestureDetector(
                                    child: buildGridItem(
                                            name: itemBean.name, iconAssets: itemBean.res),
                                    onTap: () {
                                        itemBean.onItemClick(itemBean.name);
                                    },
                                );
                            }),
                ),
            ),
        );
    }

    Padding buildLabel(String lableName) {
        return Padding(
            child: Text(
                lableName,
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            padding: EdgeInsets.only(left: 20, bottom: 10),
        );
    }

    Row buildTitleBar() {
        return Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
                Container(
                    child: Text(
                        '员工',
                        style: TextStyle(
                                fontSize: 18,
                                fontWeight: FontWeight.normal,
                                color: Colors.white),
                    ),
                    height: 50,
                    alignment: Alignment.center,
                )
            ],
        );
    }

    Container buildHeaderInfo() {
        return Container(
                height: 170,
                decoration:
                BoxDecoration(image: DecorationImage(image: AssetImage(Res.bg_2))),
                child: Padding(
                    padding: EdgeInsets.only(left: 35, right: 35),
                    child: Row(
                        children: <Widget>[
                            Image.asset(
                                Res.icon_picture_1,
                                width: 49,
                                height: 49,
                            ),
                            SizedBox(
                                width: 15,
                            ),
                            Expanded(
                                child: Column(
                                    children: <Widget>[
                                        Text(
                                            "蒙娜丽莎",
                                            style: TextStyle(fontSize: 14, color: Color(0xFF333333)),
                                        ),
                                        SizedBox(
                                            height: 10,
                                        ),
                                        Text(
                                            "济南道珉供应链",
                                            style: TextStyle(
                                                    fontSize: 18,
                                                    color: Color(0xFF333333),
                                                    fontWeight: FontWeight.w700),
                                        ),
                                    ],
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                ),
                            ),
                            Image.asset(
                                Res.icon_arrow,
                                width: 9,
                                height: 16,
                            )
                        ],
                    ),
                ));
    }

    Widget buildCard(List<Widget> cardItems) {
        return Container(
            margin: EdgeInsets.only(left: 10, right: 10),
            child: Card(
                shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(21))),
                child: Padding(
                    padding: EdgeInsets.only(left: 22, right: 22, top: 10, bottom: 10),
                    child: Row(
                        children: cardItems,
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                    ),
                ),
            ),
        );
    }

    Widget buildCardItem(
            {String name = '暂无内容', String iconAssets = Res.icon_bargain}) {
        return Expanded(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                        Image.asset(
                            iconAssets,
                            width: 50,
                            height: 50,
                        ),
                        SizedBox(
                            height: 10,
                        ),
                        Text(
                            name,
                            style: TextStyle(fontSize: 14, fontWeight: FontWeight.w300),
                        )
                    ],
                ));
    }

    Widget buildGridItem(
            {String name = '暂无内容', String iconAssets = Res.icon_bargain}) {
        return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
                Image.asset(
                    iconAssets,
                    width: 30,
                    height: 30,
                ),
                SizedBox(
                    height: 12,
                ),
                Text(
                    name,
                    style: TextStyle(fontSize: 14, fontWeight: FontWeight.w300),
                )
            ],
        );
    }
}

typedef OnItemClick = void Function(String name);

class GridItem {
    String name;
    String res;

    OnItemClick onItemClick;

    GridItem(this.name, this.res,this.onItemClick);

}